# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor:  TDY <tdy@archlinux.info>
# Contributor: Rémy Oudompheng <oudomphe@clipper.ens.fr>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - rm -fr "$pkgdir"/usr/lib/gap/pkg/digraphs-*/bin/lib/ for aarch64
#  - strip -m32 flags

pkgbase=gap
pkgname=(gap
         gap-packages)
pkgver=4.13.1
pkgrel=1
pkgdesc='Groups, Algorithms, Programming: a system for computational discrete algebra'
arch=(x86_64)
url='https://www.gap-system.org/'
license=(GPL-2.0-or-later)
source=(https://github.com/gap-system/gap/releases/download/v$pkgver/$pkgbase-$pkgver.tar.gz
        package-infos-$pkgver.json.gz::https://github.com/gap-system/gap/releases/download/v$pkgver/package-infos.json.gz)
sha256sums=('9794dbdba6fb998e0a2d0aa8ce21fc8848ad3d3f9cc9993b0b8e20be7e1dbeba'
            '8e6bd0fd2b36f13322ea1e484b5839090b4151d2e24cc7477ad62677bc2a2701')
makedepends=(bliss
             boost
             c-xsc
             cddlib
             chrpath
             eigen
             fmt
             fplll
             givaro
             jq
             libsemigroups
             libxaw
             mpfi
             normaliz
             planarity
             wget
             zeromq)

prepare() {
  cd gap-$pkgver

# Use system normaliz
  rm pkg/normalizinterface/prerequisites.sh
# Use system nauty
  rm -r pkg/grape/{configure,Makefile*,nauty*}
# Fix https://bugs.archlinux.org/task/55174
  sed -e '/xgap/d' -i pkg/sonata/PackageInfo.g
  sed -e '/XGAP/d' -i pkg/cryst/PackageInfo.g
# Strip -m32 flags
  sed -e '/-m32/d' -i bin/BuildPackages.sh
}

build() {
  cd gap-$pkgver
  ./configure --prefix=/usr --with-gmp=system
  make

  # Install libgap so we can link packages against it
  mkdir -p tmp-install
  make DESTDIR="$srcdir"/gap-$pkgver/tmp-install install-libgap

  cd pkg
  export CFLAGS+=" -Wno-incompatible-pointer-types" # Fix build with GCC 14
  export CXXFLAGS+=" -std=gnu++14" # Fix build of normalizinterface
  export LDFLAGS+=" -L$srcdir/gap-$pkgver/tmp-install/usr/lib -lgap" # See https://trac.sagemath.org/ticket/27372
  export LD_LIBRARY_PATH="$srcdir"/gap-$pkgver/tmp-install/usr/lib
  ../bin/BuildPackages.sh --strict \
    --add-package-config-Semigroups --with-external-libsemigroups \
    --add-package-config-Digraphs "--with-external-planarity --with-external-bliss"
}

_standardpkgs=(gapdoc primgrp smallgrp transgrp atlasrep autpgrp alnuth crisp ctbllib factint fga irredsol laguna
               polenta polycyclic radiroot resclasses sophus tomlib utils autodoc)

package_gap() {
  depends=(glibc
           gmp
           perl
           readline
           sh
           zlib)
  optdepends=('gap-packages: extra packages')
  conflicts=(gap-doc)
  provides=(gap-doc)
  replaces=(gap-doc)
  cd gap-$pkgver

  make DESTDIR="$pkgdir" install

  for _pkg in ${_standardpkgs[@]}; do
    cp -r pkg/$_pkg "$pkgdir"/usr/share/gap/pkg
  done

# Add provides for bundled packages
  for _pkg in $(ls "$pkgdir"/usr/share/gap/pkg); do
    _ver=$(jq .[\"${_pkg}\"].Version "$srcdir"/package-infos-$pkgver.json | sed -e 's/"//g')
    _prov="gap-$_pkg=${_ver/-/.}"
    provides+=($_prov)
  done
}

package_gap-packages() {
  depends=(gap
           gcc-libs
           glibc
           perl
           sh)
  optdepends=('bliss: digraph package'
              'c-xsc: float package'
              'cddlib: CddLib interface package'
              'curl: curl interface package'
              'fplll: float package'
              'libmpc: float package'
              'libsemigroups: semigroups package'
              'libxaw: xgap package'
              'mpfi: float package'
              'nauty: grape package'
              'normaliz: NormalizInterface package' 
              'pari: alnuth package'
              'planarity: digraph package'
              'polymake: polymaking package'
              'singular: singular package'
              'zeromq: ZeroMQ interface package')
  pkgdesc='Extra packages for GAP'
  cd gap-$pkgver

  mkdir -p "$pkgdir"/usr/{bin,lib/gap}
  cp -r pkg "$pkgdir"/usr/lib/gap
  chmod 755 "$pkgdir"/usr/lib/gap/pkg

# fix xgap launch script
  sed -e "s|/build/gap/src/gap-$pkgver|/usr/lib/gap|g" -e 's|^GAP=.*|GAP=/usr/lib/gap/gap|g' \
    "$pkgdir"/usr/lib/gap/pkg/xgap/xgap.sh > "$pkgdir"/usr/bin/xgap
  chmod 755 "$pkgdir"/usr/bin/xgap
  rm "$pkgdir"/usr/lib/gap/pkg/xgap/xgap.sh

# provided by main gap package
  for _pkg in ${_standardpkgs[@]}; do
    rm -r "$pkgdir"/usr/lib/gap/pkg/$_pkg
  done

# fix RPATH
  find "$pkgdir"/usr/lib/gap/pkg/ -name '*.so' | xargs chrpath -d

# use system nauty in grape package
  install -d "$pkgdir"/usr/lib/gap/pkg/grape/bin/x86_64-pc-linux-gnu-default64-kv8
  ln -s /usr/bin/dreadnaut "$pkgdir"/usr/lib/gap/pkg/grape/bin/x86_64-pc-linux-gnu-default64-kv8

# remove leftover binaries and source files
  find "$pkgdir"/usr/lib/gap/pkg -name .libs -o -name '*.o' | xargs rm -fr
  find "$pkgdir"/usr/lib/gap/pkg -type d -name src | xargs rm -fr
  rm -r "$pkgdir"/usr/lib/gap/pkg/digraphs/extern
  rm -r "$pkgdir"/usr/lib/gap/pkg/semigroups/libsemigroups
  rm -r "$pkgdir"/usr/lib/gap/pkg/caratinterface/carat*
  rm -r "$pkgdir"/usr/lib/gap/pkg/kbmag/standalone
  rm -r "$pkgdir"/usr/lib/gap/pkg/log
  rm -r "$pkgdir"/usr/lib/gap/pkg/*/gen

# Add provides for bundled packages
  for _pkg in $(ls "$pkgdir"/usr/lib/gap/pkg); do
    _ver=$(jq .[\"${_pkg}\"].Version "$srcdir"/package-infos-$pkgver.json | sed -e 's/"//g')
    _prov="gap-$_pkg=${_ver/-/.}"
    provides+=($_prov)
  done
}
